package model

import (
	"errors"
	"go.uber.org/zap"
	"goskeleton/app/global/variable"
)

type ReviewModel struct {
	BaseModel
	OrderId        int    `json:"order_id"`
	ReviewerOpenId string `json:"reviewer_open_id"`
	ReviewedOpenId string `json:"reviewed_open_id"`
	Rating         int    `json:"rating"`
	Comment        string `json:"comment"`
	Status         int    `json:"status"`
}

func (ReviewModel) TableName() string {
	return "tb_review"
}

func CreateReviewFactory(sqlType string) *ReviewModel {
	return &ReviewModel{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
}

func (m ReviewModel) CreateReview(review ReviewModel) error {
	if review.OrderId <= 0 {
		return errors.New("订单ID无效")
	}

	result := m.Create(&review)
	if result.Error != nil {
		//打印失败
		variable.ZapLog.Error("解析JSON数据时出错", zap.Error(result.Error))
		return result.Error
	}

	return nil
}
